import React, { useState } from 'react'
import store, { decrementAction, incrementAction } from '../../store'

export default function CounterUpdate() {
  const [num, setNum] = useState(3)

  return (
    <div>
      <select value={num} onChange={e => setNum(+e.target.value)}>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
      <button onClick={() => store.dispatch(incrementAction(num))}>增加</button>
      <button onClick={() => store.dispatch(decrementAction(num))}>减少</button>
      <button onClick={() => {
        if (store.getState().count%2==1) {
          store.dispatch(incrementAction(num))
        }
      }}>奇数增加</button>
      <button onClick={() => {
        setTimeout(() => {
          store.dispatch(incrementAction(num))
        }, 1000);
      }}>延迟1秒增加</button>
    </div>
  )
}
